Skip to content

feat(media): set a video bitrate, average or CBR (#1616) - #1979

Draft
cre8ive-marketing wants to merge 3 commits into
KnifeLemon:mainfrom
cre8ive-marketing:feature/1616-video-bitrate
Draft

cre8ive-marketing wants to merge 3 commits into
KnifeLemon:mainfrom
cre8ive-marketing:feature/1616-video-bitrate

Conversation

@cre8ive-marketing

Copy link
Copy Markdown
Collaborator

Closes #1616

What changes

  • New fields: MediaOptions.VideoBitrateKbps (0 = off) and MediaOptions.ConstantBitrate.
  • Editor: a Video bitrate (Mbps) box next to the size limit, with a Constant bitrate (CBR) check box. It's shown where the size limit is (ShowVideoMaxSize), so not for clips, joins, slideshows, VOB or DV.
  • Average: reuses MediaEncoding.AtBitrate (-b:v N -maxrate 1.5N -bufsize 2N), now with a constant flag.
  • CBR: -b:v N -minrate N -maxrate N -bufsize N for libx264 and libx265. x264 also gets -x264-params nal-hrd=cbr.
    • Any other encoder falls back to the average, even from a hand-edited preset.
    • In the editor the CBR box is greyed unless the codec is H.264 or HEVC. Its tooltip says "For H.264 and HEVC for now", not that the codec can't do CBR.
  • Hardware encoding: with a bitrate set, HardwareEncoding is turned off, as ForSizeLimit does.
  • Never copied: CanCopyVideo is false while a bitrate is set, so the video is always encoded.
  • Size limit: when the limit needs a lower bitrate, the lower one wins and the file gets notice.bitrate_lowered_for_size. A second fit pass works as before.
  • Copied routes:
    • Clips: the bitrate box isn't shown for clips.
    • Splits: with "parts" chosen, the box is greyed, with the reason (presets.video_bitrate_split), and the bitrate isn't saved.
    • DVD / DV: left out. The editor doesn't show the box, and the engine ignores the field for vob/dv.
  • Gallery: videoBitrateKbps (IntRule 0–200000) and constantBitrate are in SharedPresetRules, and both are in SharedPresetVersions.Fields as 2.6.0.
  • i18n: new labels and the notice are in all 23 files.

Tests (VideoBitrateTests)

  • Arguments: CBR for x264 and x265, and the average for VP9.
  • No copy: a set bitrate never copies the video.
  • Real FFmpeg: a generated 10 s 640×360 clip, with the video stream measured from its packets.
    • The 2 Mbps average lands within ±10%.
    • CBR lands within ±5%.
    • Hardware encoding was asked for, and is ignored.
  • Size limit wins: 5 Mbps asked with a 1 MB limit gives a file under 1 MB, with the notice.

Also run

  • SizeLimitTests and Core tests: all pass.
  • App tests for the preset editor, localization and gallery: all pass.
  • Not run: the full App suite, and a look at the editor in the real app.

Video presets get MediaOptions.VideoBitrateKbps and ConstantBitrate. The
video is then encoded at that bitrate in software (never copied), with
MediaEncoding.AtBitrate's average shape or CBR for x264/x265. A size
limit that needs less wins, with notice.bitrate_lowered_for_size.
Splits grey the option; VOB and DV keep their fixed rates. The gallery
allow-list and SharedPresetVersions know both fields (2.6.0).

Closes KnifeLemon#1616
@github-actions github-actions Bot added the feature New features label Dec 2, 2026

@KnifeLemon KnifeLemon left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @cre8ive-marketing, this follows the plan closely:

  • AtBitrate is reused;
  • hardware encoding is turned off and there is no stream copy;
  • DVD/DV are left out;
  • the gallery fields are in.

A few small things before it's ready:

  • FitAgainAsync (FfmpegConverter.cs:322) re-encodes with AtBitrate(..., kbps) without constant. A CBR preset whose first encode misses the size limit then comes out at an average bitrate on the second pass. And when the set bitrate is lower than the limit's, that pass starts from the limit's kbps rather than the set one.
  • Apply() (PresetEditorViewModel.cs:1581) keeps ConstantBitrate when the box is greyed. To see it, tick CBR on H.264, then switch to VP9. The engine falls back, so nothing breaks, but && CanConstantBitrate would keep the saved preset honest.
  • Tests: the hardware case sets HardwareEncoding = true, but nothing checks which encoder ran. The editor's "parts" case (the bitrate isn't saved) has no test.
  • For H.265, ffmpeg's libx265 wrapper ignores -minrate. So "CBR" there is an average bitrate capped by VBV, not strict CBR. Either add strict-cbr=1 to the existing -x265-params, or word the option so it doesn't promise more than that.
  • CHANGELOG.md conflicts with main now.

# Conflicts:
#	CHANGELOG.md
…or keeps the preset honest

- The second encode for a size limit starts from the set bitrate and stays CBR when asked.
- x265 gets strict-cbr=1, since libx265 ignores -minrate.
- The editor saves CBR only where the codec takes it.
- Tests: no GPU encoder is looked for with a bitrate set; parts save no bitrate; CBR not saved for AV1.
@cre8ive-marketing

Copy link
Copy Markdown
Collaborator Author

Thanks, done in a3071a4:

  • FitAgainAsync now starts from the set bitrate (not the measured one) and keeps CBR when it is on.
  • libx265 with CBR adds strict-cbr=1 to -x265-params. Checked with real FFmpeg: 500k asked gives about 505 kbps.
  • The editor saves ConstantBitrate only when a bitrate is set and the codec can do CBR, so AV1 saves no CBR.
  • New tests: parts save no bitrate, AV1 saves no CBR, and a set bitrate encodes in software even with hardware encoding on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: 视频转码增加自定义目标码率输入功能

2 participants